// pages/repairs/repairs.js
var http = require("../../utils/http.js");
var dateTimePicker = require('../../utils/dateTimePicker.js');

Page({
  data: {
    time: '',
    imgs: [],
    title:'点击选择图片'
  },

  // 上传图片
  chooseImg: function (e) {
    var that = this;
    var imgs = this.data.imgs;
    if (imgs.length >= 9) {
      this.setData({
        lenMore: 1
      });
      setTimeout(function () {
        that.setData({
          lenMore: 0
        });
      }, 2500);
      return false;
    }
    wx.chooseImage({
      // count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        var imgs = that.data.imgs;
        // console.log(tempFilePaths + '----');
        for (var i = 0; i < tempFilePaths.length; i++) {
          if (imgs.length >= 9) {
            that.setData({
              imgs: imgs
            });
            return false;
          } else {
            imgs.push(tempFilePaths[i]);
          }
        }
        console.log(imgs);
        that.setData({
          imgs: imgs,
          title:''
        });
      }
    });
  },

  // 删除图片
  deleteImg: function (e) {
    this.setData({
      title:'点击选择图片'
    })
    var imgs = this.data.imgs;
    var index = e.currentTarget.dataset.index;
    imgs.splice(index, 1);
    this.setData({
      imgs: imgs
    });
  },

  // 预览图片
  previewImg: function (e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    var imgs = this.data.imgs;
    wx.previewImage({
      //当前显示图片
      current: imgs[index],
      //所有图片
      urls: imgs
    })
  },

  //选择时间
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },

onLoad: function(options) {
  var now = new Date();
  this.setData({
    time:now.getFullYear()+'-'+(now.getMonth()+1)+'-'+now.getDate()
  })
  },

  //提交
  onSubmit(e){
    console.log(e.detail.value)
    var a=e.detail.value
    var e1=true;
    if(a.goods==''||a.place==''||a.content=='')
    {
      e1=false;
    }
    if(e1){
      var imgs = this.data.imgs;
      if(imgs.length==0)
      {
        wx.showToast({
          title: '请选择图片',
          icon: 'error',
          duration: 2000
        })
      }else{
        wx.showLoading({
          title: '加载中',
        })
      }
      for(var item of imgs)
      {
        // console.log(item)
        wx.uploadFile({
          url: 'http://localhost:5000/api/Image/UploadImg', //仅为示例，非真实的接口地址
          filePath: item,
          name: 'file',
          success:(data)=>{
            // console.log(JSON.parse(data.data).data)
            // console.log(this.data)
            http.post(
              'api/Repairs/AddRepairs?id='+JSON.parse(wx.getStorageSync('Security')).id,
              {
                ...e.detail.value,
                picture:JSON.parse(data.data).data,
                beginTime:this.data.time
              },
              (data)=>{
                wx.hideLoading();
                if(data.code==200){
                wx.switchTab({
                  url: '/pages/index/index'
                })
                wx.showToast({
                  title: data.message,
                  icon: 'none',
                  duration: 1500
              })
                }else{
                  wx.showToast({
                      title: data.message,
                      icon: 'none',
                      duration: 1500
                  })
                }
              }
              )
          }
        })
      }
    }else{
      wx.showToast({
        title: '请把信息填写完整！',
        icon:'none',
        duration:1000
      })
    }
  }
})